package com.crm.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Transaction;

import com.crm.dao.base.BaseHibernateDAO;
import com.crm.po.Kc;


public class KcDAO extends BaseHibernateDAO {

	private static Logger logger = Logger.getLogger(KcDAO.class);

	public void save(Kc kc) {
		if(kc.getTid() == null || kc.getTid().trim().length() == 0){
			kc.setTid(this.getUUID());
		}
		Transaction transaction = getSession().beginTransaction();
		try {
			this.getSession().save(kc);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}

	public void saveOrUpdate(Kc kc) {
		if(kc.getTid() == null || kc.getTid().trim().length() == 0){
			kc.setTid(this.getUUID());
		}
		Transaction transaction = getSession().beginTransaction();
		try {						
			this.getSession().saveOrUpdate(kc);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}
	
	public void delete(Kc kc) {
		Transaction transaction = getSession().beginTransaction();
		try {
			this.getSession().delete(kc);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}

	public Kc findById(String id) {
		Kc kc = null;
		try {
			kc = (Kc) this.getSession().get(Kc.class, id);
		} catch (RuntimeException e) {
			logger.error(e);
		} finally {
			this.closeSession();	
		}
		return kc;
	}
	
	@SuppressWarnings("unchecked")
	public List<Kc> kcList(String hql) {
		return (List<Kc>) findList(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<Kc> kcListPage(String hql, int start, int limit) {
		return (List<Kc>) findListPage(hql, start, limit);
	}

}